<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * post_controller_constructor
 *
 * @author Simon Emms <simon@simonemms.com>
 */
class post_controller_constructor {
    
    
    
    
    
    
    /**
     * Add UI
     * 
     * Get the UI from the config
     */
    public function add_ui() {
        $objCI = &get_instance();
        
        $objCI->load->config('user_interface');
        $objCI->load->library('user_interface');
        
        $arrUI = $objCI->config->item('user_interface');
        
        if(is_array($arrUI) && count($arrUI) > 0) {
            foreach($arrUI as $type => $value) {
                if(is_array($value) && count($value) > 0) {
                    $type = strtolower($type);
                    foreach($value as $location) {
                        $objCI->user_interface->{$type}($location);
                    }
                }
            }
        }
    }
    
    

    
    
    
    /**
     * Check Login
     * 
     * Check that we are logged in 
     */
    public function check_login() {
        /* Get the instance */
        $objCI = &get_instance();
        
        /* Load the config */
        $objCI->load->config('login');
        $arrNoLogin = $objCI->config->item('arrNoLogin');
        $arrIgnoreLogin = $objCI->config->item('arrIgnoreLogin');
        
        /* Load the login model */
        $objCI->load->model('database');
        
        $arrPage = array_values($objCI->router->_strip_hyphens($objCI->uri->segment_array()));
        
        /* Are we testing the login on this page */
        if(is_array($arrIgnoreLogin)) {
            if(array_search($arrPage, $arrIgnoreLogin) !== false) {
                /* Yes - don't go any further */
                return;
            }
        }
        
        /* Is this a page we need to be logged in for */
        $login_page = true;
        
        if(is_array($arrNoLogin)) {
            if(array_search($arrPage, $arrNoLogin) !== false) {
                /* Don't need to be logged in */
                $login_page = false;
            }
        }
      
        /* Check if we should be logged in */
        if($login_page) {
            /* Yes we should - verify the session data */
            $logged_in = $objCI->database->verify_saved_login();
            if($logged_in === false) {
                /* Not logged in - clear the session */
                $objCI->database->logout();
                
                /* Is it an AJAX request */
                if($objCI->input->is_ajax_request()) {
                    /* Yes - output JSON string and stop there */
                    echo json_encode(array(
                        'logged_in' => false,
                    ));
                    exit;
                } else {
                    /* Save the page */
                    $objCI->session->set_userdata($objCI->config->item('session_redirect'), current_url());

                    /* Return to homepage */
                    redirect(site_url('/login'));
                }
            }
        } else {
            $logged_in = $objCI->database->verify_saved_login();
            if($logged_in !== false) {
                $referrer = $objCI->session->flashdata($objCI->config->item('session_referrer'));
                
                /* Doesn't exist - back to homepage */
                if($referrer === false) { $referrer = site_url(); }
                
                redirect($referrer);
            }
        }
    }
    
    
    
    
    
    
    
    
    /**
     * Fetch Saved Lang
     * 
     * Fetch the save language and set the site
     * language to that 
     */
    public function fetch_saved_lang() {
        $objCI = &get_instance();
        
        /* Has a language been specified in the URL */
        if($objCI->lang->lang_set() === false) {
            /* Load stuff */
            $objCI->load->library('session');
            
            /* Check session/cookie */
            $name = $objCI->lang->save_name;
            
            $lang = $objCI->session->userdata($name);
            
            if($lang === false) {
                /* Session doesn't exist - find the cookie */
                $lang = $objCI->input->cookie($objCI->lang->save_name);
            }
            
            /* Check it's a valid language */
            $arrLangs = $objCI->config->item('languages');
            
            if(is_string($lang) && in_array($lang, $arrLangs)) {
                /* Valid language - set it */
                $objCI->config->set_item('language', $lang);
            }
        }
    }
    
    
    
}

?>